package com.begonzales.locker.mapper;

import com.begonzales.locker.util.HelperFunc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


public class CTable {


	private String str2 = "    <resultMap id=\"ResultMap\" type=\"com.nike.brand.thelocker.domain.allotment.AllotmentType\">"+
			"$$body$$"+
			"resultMap";
	public String tableName;
    public String tableClassMapperPath;
	public String tableClassPath;
    public String mapperObject = "";
	
	public ArrayList<CTableColumns> cols;

    public CTable()
    {
        cols = new ArrayList<CTableColumns>();
    }

	public void writeToFileMapper(String fileName)
	{
		try {
            File removeFile = new File(fileName);
            if ( removeFile.exists())
            {
                removeFile.delete();
            }
		    BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
		    out.write( emitMapper() );
		    out.close();
		} catch (IOException e) {
		}		
	}
    public void writeToFileEntity(String fileName, String classType)
    {
        try {
            File removeFile = new File(fileName);
            if ( removeFile.exists())
            {
                removeFile.delete();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write( getFileEntityJava(classType) );
            out.close();
        } catch (IOException e) {
        }
    }
    public void writeToFileInterface(String fileName, String classType)
    {
        try {
            File removeFile = new File(fileName);
            if ( removeFile.exists())
            {
                removeFile.delete();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write( this.getFileInterfaceJava(classType) );
            out.close();
        } catch (IOException e) {
        }
    }

    public String emitMapper()
    {
        String mapperBegin = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
                "<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n"+
                "<mapper namespace=\""+this.tableClassMapperPath+"\"> \r\n\r\n";
        String mapperEnd = "\r\n</mapper>";

        String retval = mapperBegin
                + this.getResultMap()
                + this.sqlTable()
                + this.sqlIdColumn()
                + this.sqlColumns()
                + this.sqlBaseSelect()
                + this.sqlGetAll()
                + this.selectByPrimaryKey("Long")
                + this.sqlInsert()
                + this.sqlUpdate()
                + this.sqlDelete()
                + mapperEnd ;
                //+ getPrivateVars()
                //+ getPrivateSetterGetterVars();
        return( retval );
    }

    public String getFileInterfaceJava(String className)
    {
        String retval = "";

        retval = retval + "" +
                "package com.nike.brand.thelocker.mapper.transaction;\n" +
                "\n" +
                "import com.nike.brand.thelocker.domain.transaction."+ HelperFunc.firstLetterCap(className)+";\n" +
                "import com.nike.brand.thelocker.mapper.GenericCRUDMapper;\n" +
                "import org.springframework.stereotype.Component;" +
                /*
                "\n =============" +
                "package com.nike.brand.thelocker.domain; \r\n\r\n" +
                "import com.nike.brand.thelocker.domain.BaseEntity; \r\n" +
                "import com.nike.brand.thelocker.mapper.GenericCRUDMapper; \r\n" +
                "import org.apache.ibatis.annotations.Param; \r\n" +
                "import org.springframework.stereotype.Component; \r\n\r\n" +
                */
                "/** \r\n" +
                " *  MyBatis mapper for [DomainNameHere] \r\n" +
                " *  @author [YourNameHere] \r\n" +
                " */  \r\n\r\n" +
                "@Component\r\n" +
                "public interface " + className + " extends GenericCRUDMapper<"+className+"> \r\n" +
                "{ \r\n" +
                "} \r\n" ;

        return retval;
    }


    public String getFileEntityJava(String className)
    {
        String retval = "";

        retval = retval + "" +
                "package com.nike.brand.thelocker.domain; \r\n\r\n" +
                "import com.nike.brand.thelocker.domain.BaseEntity; \r\n\r\n" +
                "public class " + className + " extends BaseEntity \r\n" +
                "{ \r\n" +
                this.getPrivateVars() +
                this.getPrivateSetterGetterVars() +
                "} \r\n" ;

        return retval;
    }

    public String getPrivateVars()
    {
        String retval = "";
        for(CTableColumns item : cols)
        {
                retval = retval +  "\tprivate " + item.javaType + " " +  item.columnMetaName + ";\r\n";
        }
        retval = retval +  "\r\n";
        return(retval);
    }
    private String firstLetterCap(String myString)
    {
        char[] stringArray = myString.toCharArray();
        stringArray[0] = Character.toUpperCase(stringArray[0]);
        return(new String(stringArray));
    }

    public String getPrivateSetterGetterVars()
    {
        String retval = "";
        for(CTableColumns item : cols)
        {
            if (item.isPrimaryKey == false)
            {
                retval = retval +  "\tprivate void set" +  this.firstLetterCap(item.columnMetaName) + "(" + item.javaType + " " + item.columnMetaName + " )\r\n" +
                        "\t{\r\n" +
                        "\t\tthis." +  item.columnMetaName + " = " + item.columnMetaName + ";\r\n" +
                        "\t}\r\n";
                retval = retval +  "\tprivate "+item.javaType+" get" +  this.firstLetterCap(item.columnMetaName) + "()\r\n" +
                        "\t{\r\n" +
                        "\t\treturn " + item.columnMetaName + ";\r\n" +
                        "\t}\r\n";
            }
        }
        return(retval);
    }

    public String sqlTable()
    {
        String retval = "";
        int ctr = 0;

        retval = retval + "\t<sql id=\"table\">"+this.tableName+"</sql>\r\n";

        return(retval);
    }
    public String sqlIdColumn()
    {
        String retval = "";
        int ctr = 0;

        retval = retval + "\t<sql id=\"idColumn\">"+this.getPrimaryKey().columnFieldName+"</sql>\r\n";

        return(retval);
    }

    public CTableColumns getPrimaryKey()
    {
        for(CTableColumns item : cols)
        {
            if ( item.isPrimaryKey )
            {
                return item;
            }
        }
        return null;
    }

    public String sqlColumns()
    {
        String retval = "";
        int ctr = 0;
        retval = retval + "\t<sql id=\"columns\"> ";
        for(CTableColumns item : cols)
        {
            if ( 0 == ctr )
            {
                retval = retval +  item.columnFieldName;
            }
            else
            {
                retval = retval + "," + item.columnFieldName;
            }
            ctr++;
        }
        retval = retval +  " </sql>\r\n";
        return(retval);
    }

    public String sqlBaseSelect()
    {
        String retval = "";
        int ctr = 0;
        retval = retval + "\t<sql id=\"baseSelect\"> select <include refid=\"columns\"/> from <include refid=\"table\"/>";
        retval = retval +  " </sql>\r\n";
        return(retval);
    }

    public String sqlGetAll()
    {
        String retval = "";
        int ctr = 0;
        //retval = retval + "\t<sql id=\"getAll\" resultMap=\"ResultMap\"  <include refid=\"baseSelect\"/></sql>\r\n";
        retval = retval + "    <select id=\"getAll\" resultMap=\"ResultMap\"><include refid=\"baseSelect\" /></select>";
        return(retval);
    }

    public String selectByPrimaryKey(String parameterType)
    {
        String retval = "";
        int ctr = 0;

        retval = retval + "\t<select id=\"selectByPrimaryKey\" parameterType=\""+parameterType+"\" resultMap=\"ResultMap\"  >\r\n";
        retval = retval +  "\t\t<include refid=\"baseSelect\"/>";
        retval = retval +  " WHERE <include refid=\"idColumn\"/> = #{id,jdbcType="+this.getPrimaryKey().columnFieldType+"} \r\n";
        retval = retval +  "\t</select>\r\n";
        return(retval);
    }

	public String getResultMap() 
	{
		String retval = "";
		int ctr = 0;

		retval = retval + "\t<resultMap id=\"ResultMap\" type=\""+this.tableClassPath+"\">\r\n";

		for(CTableColumns item : cols)
		{
			if ( 0 == ctr )
			{
				retval = retval +  item.emitResultMapColId();
			}
			else
			{
				retval = retval +  item.emitResultMapColResult();
			}
            ctr++;
		}
		retval = retval +  "\t</resultMap>\r\n";
		return(retval);
	}
	
    public String sqlInsert()
    {
        String retval = "";
        int ctr = 0;
        retval = retval + "\t<insert id=\"insert\" parameterType=\""+this.tableClassPath+"\">   \r\n";
        retval = retval + "\t<selectKey keyProperty=\"id\" resultType=\"Long\" order=\"BEFORE\">select "+this.tableName+"_SEQ.NEXTVAL from DUAL</selectKey>   \r\n";

        retval = retval + "\t\tINSERT INTO " + "<include refid=\"table\"/>" + " ( ";
        for(CTableColumns item : cols)
        {
            if ( 0 == ctr )
            {
                retval = retval +  item.columnFieldName;
            }
            else
            {
                retval = retval + "," + item.columnFieldName;
            }
            ctr++;
        }
        retval = retval +  " ) \r\n\t\tVALUES ( " ;
        ctr=0;
        for(CTableColumns item : cols)
        {
            if ( 0 == ctr )
            {
                retval = retval +  "\r\n\t\t\t#{" + item.columnMetaName + ",jdbcType=" + item.columnFieldType + "}" ;
            }
            else
            {
                retval = retval +  "\r\n\t\t\t,#{" + item.columnMetaName + ",jdbcType=" + item.columnFieldType + "}" ;
            }
            ctr++;
        }
        retval = retval +  " ) " ;
        retval = retval +  "\r\n\t</insert>\r\n";
        return(retval);
    }

    public String sqlUpdate()
    {
        String retval = "";
        int ctr = 0;
        retval = retval + "\t<update id=\"update\" parameterType=\""+this.tableClassPath+"\">   \r\n";

        retval = retval + "\t\tUPDATE " + "<include refid=\"table\"/>" + " SET ";
        for(CTableColumns item : cols)
        {
            if ( 0 == ctr )
            {
                retval = retval + "\r\n\t\t\t " +  item.columnFieldName + " = #{" + item.columnMetaName + ",jdbcType=" + item.columnFieldType + "}";
            }
            else
            {
                retval = retval + "\r\n\t\t\t," + item.columnFieldName + " = #{" + item.columnMetaName + ",jdbcType=" + item.columnFieldType + "}";
            }
            ctr++;
        }
        retval = retval + "\r\n\t\tWHERE " + "<include refid=\"idColumn\"/>" + " = #{id,jdbcType="+this.getPrimaryKey().columnFieldType+"}";
        retval = retval + "\r\n\t</update>\r\n";
        return(retval);
    }

    public String sqlDelete()
    {
        String retval = "";
        int ctr = 0;
        retval = retval + "\t<delete id=\"delete\"> \r\n\t delete from <include refid=\"table\"/> where <include refid=\"idColumn\"/>" + " = #{id,jdbcType="+this.getPrimaryKey().columnFieldType+"}";
        retval = retval +  "\r\n \t</delete>\r\n";
        return(retval);
    }


}
